#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>

#define err(msg) do { perror(msg); exit(EXIT_FAILURE); } while(0)

int main(int argc, char *argv[])
{
	pid_t child;
	int fd, fdp[2];
	char buf[1024];
	int r,i;
	if( argc != 2 )
	{
		printf("nnpipe file\n");
		exit(EXIT_FAILURE);
	}
	if (pipe(fdp) == -1) err("nnpipe10:pipe");
	if ((child = fork()) < 0 ) err("nnpipe10:fork");
	if (child == 0)
	{
		close(fdp[1]);
		if (dup2(fdp[0], STDIN_FILENO) < 0) err("nnpipe10:child:dup2");
		if (execl("/usr/bin/wc","/usr/bin/wc", "--", NULL) == -1) err("nnpipe10:child:execl");
		close(fdp[0]);
	}
	else
	{
		if ((fd = open(argv[1], O_RDONLY)) < 0 ) err("nnpipe10:par:open");
		close(fdp[0]);
		do
		{
			if (( r = read(fd, buf, 1024)) < 0) err("nnpipe10:par:read");
			for( i = 1 ; i < r; i+=2);
				buf[i/2] = buf[i];
			if(write(fdp[1], &buf, i/2) < 0 ) err("nnpipe10:par:read"); 
		} while(r == 1024);
		close(fd);
		close(fdp[1]);
	}
	return 0;
}
